from template import *
from collect import collect

import sys

itime = 0
if len(sys.argv) == 2:
	t = int(sys.argv[1])
elif len(sys.argv) > 2:
	print("error: should have one argument, as time step")
else:
	t = itime


amplification_factor = 80.0


data_temp = collect("/Fields/", "Phi_global")
nx = data_temp.shape[3]


dx = 0.5e-5  # unit (m)
x = np.linspace(0, nx * dx, nx)
x = x * amplification_factor


xmin = x.min()
xmax = x.max()



##inite the fig of matplotlib
fig=plt.figure(figsize=(10,10))
fig.subplots_adjust(top=0.9,bottom=0.1,wspace=0.5,hspace=0.35)

##============potential======================================================
ax=fig.add_subplot(3,1,1)

val = collect("/Fields/", "Phi_global_avg")
val_1d = val[t, 0, 0, :]
print( "potential min: ", val_1d.min() )
print( "potential max: ", val_1d.max() )

line0=ax.plot(x, val_1d, label = "$\phi$", color='#1f77b4')

ax.set_ylim(bottom = 0.0)

ax.set_ylabel("$\phi (V)$", color='#1f77b4', fontsize = label_fontsize)
ax.tick_params('y', colors='#1f77b4')




#double y axis
ax_twinx = ax.twinx()
val = collect("/Fields/", "Ex_global_avg")
val_1d = val[t, 0, 0, :]

line0=ax_twinx.plot(x, val_1d, label = r"$E_\mathrm{x}$", linestyle = linestyles[1] , color='#ff7f0e')

ax_twinx.set_ylabel("$E_x \ (V/m)$", color='#ff7f0e', fontsize = label_fontsize)
ax_twinx.tick_params('y', colors='#ff7f0e')

lines1, labels1 = ax.get_legend_handles_labels()
lines2, labels2 = ax_twinx.get_legend_handles_labels()
ax_twinx.legend(lines1 + lines2, labels1 + labels2, loc = 1, framealpha = 0.2)

#ax.grid(True)

ax.set_xlim((xmin, xmax))

ax.annotate("(a)", xy=get_axis_limits(ax), annotation_clip=False)

##============rho======================================================
ax=fig.add_subplot(3,1,2)

val = collect("/Fields/", "Rho_global_e_avg")
val_1d = val[t, 0, 0, :]
print( "Electron density: ",val_1d.min(), val_1d.max() )

line0=ax.plot(x, val_1d, label = "Electron")


val = collect("/Fields/", "Rho_global_D1_avg")
val_1d = val[t, 0, 0, :]

line0=ax.plot(x, val_1d, label = '$\mathrm{D^+}$ ion', linestyle = linestyles[1])

ax.set_xlim((xmin, xmax))
#ax.set_ylim((0.0, 1.1))
ax.set_ylim(bottom = 0.0)

ax.set_ylabel("$n\ (m^{-3})$", fontsize = label_fontsize)


#ax.grid(True)
ax.legend(framealpha = 0.2)
ax.annotate("(b)", xy=get_axis_limits(ax), annotation_clip=False)

##============ Temperature ======================================================
ax=fig.add_subplot(3,1,3)

val = collect("/Fields/", "T_global_e_avg")
val_1d = val[t, 0, 0, :]
print( "Electron Temperature: ",val_1d.min(), val_1d.max() )

line0=ax.plot(x, val_1d, label = "Electron")


#ax.grid(True)

val = collect("/Fields/", "T_global_D1_avg")
val_1d = val[t, 0, 0, :]
print( "D+1 ion temperature: ",val_1d.min(), val_1d.max() )

line0=ax.plot(x, val_1d, label = '$\mathrm{D^+}$ ion', linestyle = linestyles[1])


ax.legend(framealpha = 0.2)

ax.set_xlim((xmin, xmax))
#ax.set_ylim((ymin, 91))
ax.set_ylim(bottom = 0.0)

ax.set_xlabel("x (m)", fontsize = label_fontsize)
ax.set_ylabel("T (eV)", fontsize = label_fontsize)

ax.annotate("(c)", xy=get_axis_limits(ax), annotation_clip=False)


pdf_file_name = "Profiles" + str(t) + ".png"
fig.savefig(pdf_file_name, dpi = 300)
##fig.show()       #when the program finishes,the figure disappears
#plt.axis('equal')
#plt.show()         #The command is OK
